<?php

namespace app\index\controller;

class SettlementBase
{
    public function index()
    {
        $query = \app\model\Settlement::field(['id', 'trade_month', 'sales_id', 'amount', 'status_value', 'invoice_status_value', 'created_at'])->scope('salesman')->with(['customers'])->order('id', 'desc');

        if (request()->has('tradeMonth')) {
            $query->whereLike('trade_month', '%' . input('tradeMonth') . '%');
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['settlements' => $models]]);
    }

    public function update()
    {
        $model = \app\model\Settlement::update(input());

        if ($model->id) {
            $model = \app\model\Settlement::find($model->id);
        }

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function detail()
    {
        $query = \app\model\Settlement::field(['id', 'trade_month', 'amount', 'status_value', 'invoice_status_value', 'created_at', 'electricity_total'])->with(['customers']);

        if ($id = input('id')) {
            $query->where('id', $id);
        }

        $model = $query->find();

        return json(['code' => 20000, 'data' => ['settlement' => $model]]);
    }
}
